<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>编辑用户</title>
    <script type="text/javascript">
      Ext.onReady(function(){
        var employeeId = <%=params[:id]%>;
        var number = new Ext.form.NumberField({
          fieldLabel: '编号',
          allowBlank: false,
          blankText: "编号不能为空",
          name: 'number',
          anchor: '95%'
        });

        var name = new Ext.form.TextField({
          fieldLabel: '姓名',
          allowBlank: false,
          blankText: "用户姓名不能为空",
          name: 'name',
          anchor: '95%'
        });

        var authPassword = new Ext.form.TextField({
          fieldLabel: '授信密码',
          allowBlank: true,
          name: 'auth_password',
          anchor: '95%'
        });

        var password = new Ext.form.TextField({
          fieldLabel: '登录密码',
          allowBlank: false,
          blankText: "用户名称不能为空",
          name: 'password',
          inputType: 'password',
          anchor: '95%'
        });

        var ab = new Ext.form.TextField({
          fieldLabel: '姓名缩写',
          allowBlank: false,
          blankText: "姓名缩写",
          name: 'ab',
          anchor: '95%'
        });
        
        var loginId = new Ext.form.TextField({
          fieldLabel: '登录Id',
          allowBlank: false,
          blankText: "登录Id不能为空",
          name: 'login_id',
          anchor: '95%'
        });

        var phone = new Ext.form.TextField({
          fieldLabel: '电话',
          name: 'phone',
          anchor: '95%'
        });

        var identityCard = new Ext.form.TextField({
          fieldLabel: '身份证',
          name: 'identity_card',
          anchor: '95%'
        });

        var email = new Ext.form.TextField({
          fieldLabel: '电子邮箱',
          name: 'email',
          anchor: '95%'
        });

        var address = new Ext.form.TextField({
          fieldLabel: '地址',
          name: 'address',
          anchor: '95%'
        });

        //部门
        var departmentTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{name}', '</div></tpl>');
        var departmentStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/department/getAllDepartments?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'name'
            }])
        });

        //部门
        var department = new Ext.form.ComboBox({
          store: departmentStore,
          fieldLabel: '公司部门',
          allowBlank: false,
          blankText: "部门必须选择",
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'department_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请选择...',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '95%',
          //pageSize: 10,
          //hideTrigger: false,
          readOnly:false,
          tpl: departmentTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction: 'all',
          editable:false,
          selectOnFocus:true
        });

        //是否离职
        var quitJob = new Ext.form.RadioGroup({
          width : 120,
          fieldLabel: '是否离职',
          allowBlank: false,
          //hideLabel:true,
          //style:'margin-left:100px;margin-top:20px',
          //columns : 7,
          name:'quit_job',
          anchor: '90%',
          vertical :false,
          items:[
            {boxLabel:'是',inputValue:'是',name:'quit_job'},
            {boxLabel:'否',inputValue:'否',name:'quit_job'}
          ]
        });
        
        //角色
        var roleTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{name}', '</div></tpl>');
        var roleStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/admin/getAllRoles?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'name'
            }])
        });

        //入职日期
        var entryDate = new Ext.form.DateField({
          fieldLabel: '入职日期',
          name: 'entry_date',
          id: 'entry_date',
          format: 'Y-m-d', //格式化日期
          width: '100',
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          endDateField: 'quit_date',
          listeners: {
            select: function(field, e){
              //Ext.MessageBox.alert('提示', '提交成功');
            }
          }
        });

        var quitDate = new Ext.form.DateField({
          fieldLabel: '离职日期',
          name: 'quit_date',
          id: 'quit_date',
          format: 'Y-m-d', //格式化日期
          width: '100',
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          startDateField: 'entry_date',
          listeners: {
            select: function(field, e){
              //Ext.MessageBox.alert('提示', '提交成功');
            }
          }
        });
        
        //角色
        var role = new Ext.form.ComboBox({
          store: roleStore,
          fieldLabel: '角色',
          allowBlank: true,
          blankText: "角色必须选择",
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'role_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请选择...',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '95%',
          //pageSize: 10,
          //hideTrigger: false,
          readOnly:false,
          tpl: roleTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction: 'all',
          editable:false,
          selectOnFocus:true
        });
        
        var desc = new Ext.form.TextArea({
          fieldLabel: '描述',
          name: 'description',
          height:80,
          anchor: '95%'
        });

        var editEmployeeForm = new Ext.FormPanel({
          labelWidth: 55, // label settings here cascade unless overridden
          frame:true,
          labelAlign: 'right',
          url: '/employee/update?id=' + employeeId,
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'number'
            }, {
              name: 'login_id'
            }, {
              name: 'name'
            }, {
              name: 'password'
            }, {
              name: 'auth_password'
            }, {
              name: 'phone'
            }, {
              name: 'identity_card'
            }, {
              name: 'address'
            }, {
              name: 'email'
            }, {
              name: 'department_id'
            }, {
              name: 'role_id'
            }, {
              name: 'ab'
            }, {
              name: 'quit_job'
            }, {
              name: 'entry_date',
              type: 'date',
              dateFormat: 'Y/m/d'
            }, {
              name: 'quit_date',
              type: 'date',
              dateFormat: 'Y/m/d'
            }, {
              name: 'description'
            }]),
          bodyStyle:'padding:5px 5px 0',
          width: 600,
          //height:300,
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [number]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [loginId]
                }, {
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [password]
                }, {
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [authPassword]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [name]
                },{
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [phone]
                },{
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [identityCard]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [ab]
                },{
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [department]
                },{
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [role]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [quitJob]
                },{
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [entryDate]
                },{
                  columnWidth: .35,
                  layout: 'form',
                  border: false,
                  items: [quitDate]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [address]
                },{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [email]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: 1,
                  layout: 'form',
                  border: false,
                  items: [desc]
                }]
            }],
          buttons: [{
              text: '提交',
              iconCls: 'icon-save',
              handler: function(){
                // check form value
                if (editEmployeeForm.form.isValid()) {
                  this.disable();
                  editEmployeeForm.form.submit({
                    method: 'GET',
                    success: function(result, response){
                      if (response.result.info == "成功") {
                        //添加成功后，隐藏窗口，并刷新Grid
                        editEmployeeWin.destroy();
                      }
                      else {
                        Ext.MessageBox.alert('消息', response.result.info);
                      }
                    },
                    failure: function(result, response){
                      Ext.MessageBox.alert('提示', result.responseText);
                      editEmployeeWin.destroy();
                    }
                  });
                }
                else {
                  Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                }
              }
            },{
              text: '取消',
              iconCls: 'exit',
              handler: function(){
                editEmployeeWin.destroy();
              }
            }]
        });
        //编辑前，把数据引入到表单中。
        // load form and assign value to fields
        departmentStore.load({callback: function(record, options, success){
            if(success){
              roleStore.load({callback: function(record, options, success){
                  if(success){
                    editEmployeeForm.form.load({
                      url: '/employee/edit?id=' + employeeId,
                      method : 'GET',
                      waitMsg: '正在载入数据...',
                      success: function(form, action){
                      },
                      failure: function(form, action){
                        Ext.MessageBox.alert('提示信息', '信息加载失败');
                      }
                    });
                  }else{
                    Ext.Msg.alert('操作','失败！');
                  }
                }});
            }else{
              Ext.Msg.alert('操作','失败！');
            }
          }});
            
        editEmployeeForm.render('edit_employee_form');
      })
    </script>
  </head>
  <body>
    <div id="edit_employee_form"></div>
  </body>
</html>
